<?php

declare(strict_types=1);

namespace app\admin\command;

use app\common\model\FootballCompetitions;
use app\common\model\Matches;
use app\common\model\MatchesOdds;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class MatchesOdd extends Command
{
    //对于赛事的配置   赛事指数 赛事数据保存后执行此定时器
    //时间条件暂时未加
    protected function configure()
    {
        // 指令配置
        $this->setName('odd')
            ->addArgument('action', Argument::OPTIONAL, '参数')
            ->setDescription('赛事指数');
    }

    protected function execute(Input $input, Output $output)
    {
        //->whereTime('match_time', 'between', [date('Y-m-d', time()), date('Y-m-d', strtotime('+1 day'))])
        $action = trim($input->getArgument('action'));
        $start = strtotime(date('Y-m-d', time()) . '00:00:00');
        $end = strtotime(date('Y-m-d', time()) . '23:59:59');
        Matches::field('id,api_id,match_id,home_team_name as name,type')->where(['type' => $action])->whereTime('match_time', 'between', [$start, $end])->chunk(100, function ($competitions) use ($action, $output) {
            foreach ($competitions as $key => $competition) {
                $output->writeln('=========================循环第' . ($key + 1) . '个数据，开始循环赛事名称为:' . $competition['name'] . '=====================================');
                // 处理结果集...
                if ($action == 'football') {
                    $match_id = $competition['match_id'];
                } else {
                    $match_id = $competition['api_id'];
                }
                $url = 'https://api-f.kqiu.cn/api/v1/' . $action . '/match/odds?match_id=' . $match_id;
                $res = curl_get($url);
                $arr = json_decode($res, true);
                if ($arr['code'] == 0 && count($arr['data']) > 0) {
                    $asia = null;
                    $bs = null;
                    $cr = null;
                    $eu = null;
                    if (array_key_exists('asia', $arr['data'])) {
                        $asia = json_encode($arr['data']['asia'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    }
                    if (array_key_exists('bs', $arr['data'])) {
                        $bs = json_encode($arr['data']['bs'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    }
                    if (array_key_exists('cr', $arr['data'])) {
                        $cr = json_encode($arr['data']['cr'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    }
                    if (array_key_exists('eu', $arr['data'])) {
                        $eu = json_encode($arr['data']['eu'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    }
                    $result_arr = [
                        'foreign_key_id' => $competition['id'],
                        'type' => $action,
                        'match_id' => $match_id,
                        'asia' => $asia,
                        'bs' => $bs,
                        'cr' => $cr,
                        'eu' => $eu,
                    ];
                    $list_find = MatchesOdds::where(['foreign_key_id' => $competition['id']])->findOrEmpty();

                    if (!$list_find->isEmpty()) {
                        //更新
                        $list_find->save($result_arr);
                        $output->writeln('=========================循环第' . ($key + 1) . '个数据，' . $competition['name'] . '更新完成=====================================');
                    } else {
                        MatchesOdds::create($result_arr);
                        $output->writeln('=========================循环第' . ($key + 1) . '个数据，入库完成=====================================');
                    }

                    $output->writeln('=========================循环第' . ($key + 1) . '个数据，新增名称' . $competition['name'] . '入库完成=====================================');
                } else {
                    $output->writeln('=========================指数数据为空=====================================');
                }
            }
        });
        // 指令输出
        $output->writeln('结束');
    }
}
